from django.contrib import admin

# Register your models here.
from django.utils.html import format_html

from users.models import Following, UserProfile
from .models import Category, Match, Team, Test, JoinInfo


class CategoryAdmin(admin.ModelAdmin):
    '''设置列表可显示的字段'''
    list_display = ('name',)

    '''设置过滤选项'''
    list_filter = ('name',)

    '''搜索框 ^, =, @, None=icontains'''
    search_fields = ['name']


class MatchAdmin(admin.ModelAdmin):
    list_display = ('title', 'status', 'views')

    list_filter = ('status',)

    '''设置可编辑字段'''
    list_editable = ('status',)

    '''按发布日期排序'''
    ordering = ('-views',)

    '''搜索框 ^, =, @, None=icontains'''
    search_fields = ['title']


class FollowingAdmin(admin.ModelAdmin):
    list_display = ('target', 'follower', 'follow_date')

    '''搜索框 ^, =, @, None=icontains'''
    # search_fields = ['follower']


class JoinTimeInline(admin.TabularInline):
    model = JoinInfo
    extra = 0
    can_delete = True
    verbose_name = '添加队友信息'
    verbose_name_plural = '队友'
    autocomplete_fields = ['user']


class TeamAdmin(admin.ModelAdmin):
    list_display = ('name', 'leader_name')
    search_fields = ('name',)
    inlines = [
        JoinTimeInline
    ]
    '''中间省略'''
    autocomplete_fields = ['leader']

    def leader_name(self, obj):
        return obj.leader.nickname


class TestAdmin(admin.ModelAdmin):
    list_display = ('name', )

admin.site.register(Category, CategoryAdmin)
admin.site.register(Match, MatchAdmin)
admin.site.register(Following, FollowingAdmin)
admin.site.register(Team, TeamAdmin)
admin.site.register(Test, TestAdmin)
